package com.gzhryc.shared_device.oem.h5.mng.controllers.config;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.config.BusinessConfig;
import com.gzhryc.shared_device.oem.code.ConfigUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class BusinessConfigController {

    @Route("/config/business/setting.html")
    public String setting(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        BusinessConfig config = ConfigUtils.getBusinessConfig();
        request.setAttribute("config", config);
        return "/config/business/setting.html";
    }

    @Route(value = { "/config/business/update.do" })
    public AjaxResult updateSetting(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改失败");
        try {
            BusinessConfig config = WebUtils.getParameter(request, BusinessConfig.class);
            if (config != null){
                if(config.getServiceChargeRatio() == null || config.getServiceChargeRatio() < 0 || config.getServiceChargeRatio() >= 100) {
                    throw new LogicException("请输入正确的平台流水收益百分比");
                }
                Double reservedFundMoney = WebUtils.getDouble(request, "reservedFundMoney");
            	if(reservedFundMoney != null) {
            		config.setReservedFund(NumberTools.changeMoneyLi(reservedFundMoney));
            	}
            	if(config.getCanVirtualMoney() == null){
            		config.setCanVirtualMoney(false);
                }
                if (ConfigUtils.save(config)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            }
        } catch (Exception e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
